//
//  RBGMarqueModeleController.m
//  BateauGenerique
//
//  Created by Kevin De Jesus Ferreira on 09/06/2014.
//  Copyright (c) 2014 Kevin De Jesus Ferreira. All rights reserved.
//

#import "RBGMarqueModeleController.h"
#import "RBGAppDelegate.h"
#import "UIImageView+WebCache.h"
#import "RBGNavigationBar.h"
#import "Marque.h"
#import "RBGServiceChargement.h"
#import "RBGModeleController.h"
#import "RBGServiceChargement.h"



@interface RBGMarqueModeleController ()<UITableViewDelegate,UITableViewDataSource,RBGServiceChargementDelegate,RBGModeleDelegate>
{
    RBGAppDelegate *appDelegate;
    NSMutableArray * values;
    Marque * marque;
    bool retour;
    
    NSArray* array;
    
}


@end

@implementation RBGMarqueModeleController


- (void)viewDidLoad
{
    [super viewDidLoad];
    appDelegate = (RBGAppDelegate *)[[UIApplication sharedApplication] delegate];
    marque = nil;
    values = [[NSMutableArray alloc]init];
    
    if(self.wa){
        
        array = [appDelegate.donnees.marques objectForKey:[NSString stringWithFormat:@"%01d",self.typeAnnnonce + 1]];
        
        for(Marque * m in array){
            if(![values isKindOfClass:[Marque class]]){
                [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
                RBGServiceChargement * chargement = [[RBGServiceChargement alloc]init];
                chargement.delegate = self;
                [chargement chargerMarquesBateauType:[NSString stringWithFormat:@"%01d",self.typeAnnnonce + 1] andIdClient:nil andWa:YES];
                
                break;
            }
            [values addObject:m.libelle];
        }
        
    }else{
        
        array = [appDelegate.donnees.TOUTESmarques objectForKey:[NSString stringWithFormat:@"%01d",self.typeAnnnonce + 1]];
        
        NSLog(@"%01d",self.typeAnnnonce + 1);
        for(Marque * m in array){
            
            if(![values isKindOfClass:[Marque class]]){
                
                [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
                RBGServiceChargement * chargement = [[RBGServiceChargement alloc]init];
                chargement.delegate = self;
                [chargement chargerMarquesBateauType:[NSString stringWithFormat:@"%01d",self.typeAnnnonce + 1] andIdClient:nil andWa:NO];
                
                break;
            }
            [values addObject:m.libelle];
        }
    }
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.fond setImageWithURL:[NSURL URLWithString:appDelegate.donnees.parametres.imageFond]];
    self.tableView.backgroundColor = [UIColor clearColor];
    
    
    //[self.navigationItem.backBarButtonItem setTitle:@"teub"];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    RBGServiceChargement * chargement = [[RBGServiceChargement alloc]init];
    chargement.delegate = self;
    
    marque = [array objectAtIndex:indexPath.row];
    
    [chargement chargerModeles:marque.id andIdType:[NSString stringWithFormat:@"%01d",self.typeAnnnonce +1] andWa:self.wa andIdClient:nil];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [values count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SimpleCell"];
    
    [cell.textLabel setText:[values objectAtIndex:indexPath.row]];
    
    [cell.textLabel setTextColor:appDelegate.donnees.parametres.fontColorDeux];
    
    [cell.textLabel setFont: [UIFont fontWithName:@"Arial" size:13.0f]];
    
    UIView *selectedView = [[UIView alloc]init];
    selectedView.backgroundColor = appDelegate.donnees.parametres.backgroundColorDeux;
    cell.selectedBackgroundView =  selectedView;
    
    [cell setBackgroundColor:[UIColor clearColor]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 100 * 2;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * view = [[UIView alloc]init];
    [view setBackgroundColor:[UIColor clearColor]];
    return view;
}

- (void) receiveDonneesChargement:(id)donnees
                         andType :(int)typeDonnees{
    
    if(typeDonnees == modeles){
    RBGModeleController * controller = [[RBGModeleController alloc]init];
    
    controller.idMarque = marque.id;
    controller.modeles = donnees;
    controller.delegate = self;
    controller.filtreController = self.filtreController;
    //[self.delegate receiveValueSelected:index andTypeValeur:self.type];
    
    // Peut être pas le bon controller
    [self.filtreController.navigationController pushViewController:controller animated:YES];
   
    }else{
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

        [values removeAllObjects];
       
        array = donnees;
        for(Marque * m in donnees){
            [values addObject:m.libelle];
        }
                [self.tableView reloadData];
    }
    
}

-(void) viewDidAppear:(BOOL)animated{
    
    if(retour)
        [self.navigationController popViewControllerAnimated:NO];
    
}

-(void)viewWillAppear:(BOOL)animated{
    if(retour){
        self.navigationController.navigationBarHidden = YES;
        self.tableView.hidden = YES;
    }
    
    [self setTitle:@"Marques"];
    
    self.navigationItem.backBarButtonItem.title = @"dynamic title";
    
}
- (void) receiveDonneesRBGModele:(NSString *)idModele
                    andNomModele:(NSString *)libelleModele{
    
    [self.delegate receiveDonneesRBGMarqueModele:marque.id andNomMarque:marque.libelle andIdModele:idModele andNomModele:libelleModele];
    
    retour = YES;
}

- (void) receiveErreurChargement:(int)statut{
    // dommage
}





@end
